package net.liuzd.spring.boot.v2.domain;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;

public class ImageResult {

    private String imagePath;
    private byte[] imageData;

    public ImageResult(String imagePath) {
        this.imagePath = imagePath;
    }

    public ImageResult() {}

    public String getImagePath() {
        return imagePath;
    }

    public byte[] getImageData() throws Exception {
        if (StringUtils.isBlank(imagePath)) {
            return null;
        }
        if (imageData != null) {
            return imageData;
        }
        File file = ResourceUtils.getFile(imagePath);
        if (file == null) {
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        imageData = new byte[fi.available()];
        fi.read(imageData);
        fi.close();
        return imageData;
    }
}